package com.reactor.core;

import java.io.File;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoHandler;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.TransportType;
import org.apache.mina.transport.socket.nio.SocketSessionConfig;

import com.reactor.handler.EventDecoupler;

public class TimeServerHandler implements IoHandler {

	@Override
	public void exceptionCaught(IoSession arg0, Throwable arg1)
			throws Exception {
		// TODO Auto-generated method stub
		arg1.printStackTrace();
		arg0.close();

	}

	@Override
	public void messageReceived(IoSession arg0, Object arg1) throws Exception {
		// TODO Auto-generated method stub
		String str = arg1.toString();
		System.out.println("Server : Message recieved...: " + str);

		//File xmlFile = new File("command.xml");
		//String fileReq = FileUtils.readFileToString(xmlFile, "UTF-8");
		
		//String fileReq = FileFunctions.readTextFile("command.xml");
		//fileReq = fileReq.replaceAll("\\r\\n","");
//		fileReq = fileReq.replaceAll("\\t","");
		//arg0.write( fileReq );
		//System.out.println("Server :Message sent... : " + date.toString());
///////////////////////////////////////////////////////
		EventDecoupler d  =new EventDecoupler(arg0, arg1.toString());
		System.out.println("Server :Message sent... : ");
		

	}

	@Override
	public void messageSent(IoSession arg0, Object arg1) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void sessionClosed(IoSession arg0) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void sessionCreated(IoSession arg0) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("Server : Session created...");

//		if( arg0.getTransportType() == TransportType.SOCKET )
//			((SocketSessionConfig) arg0.getConfig() ).setReceiveBufferSize( 2048 );

//        arg0.setIdleTime( IdleStatus.BOTH_IDLE, 10 );


	}

	@Override
	public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void sessionOpened(IoSession arg0) throws Exception {
		// TODO Auto-generated method stub

	}

}
